package nettypro.netty.codec;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @ClassName NettyServerHandler
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/23 0023 16:03
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        StudentPoJO.Student student = (StudentPoJO.Student) msg;
        System.out.println("客户端发送的数据: id="+student.getId()+ ", name=" + student.getName());
    }

    /**
     * @Description 读取数据完毕
     * @Date 2020/3/23 0023 16:13
     * @param ctx
     * @return void
     **/
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端", CharsetUtil.UTF_8));
    }

    /**
     * @Description 处理异常
     * @Date 2020/3/23 0023 16:16
     * @param ctx
     * @param cause
     * @return void
     **/
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
